'use strict';
function createItemInfo(){
    var itemInfoObj = new itemContainer();
    var item = Item.all();
    for (var obj of item){
        itemInfoObj.itemInfoPush(obj);
    }
    return itemInfoObj;
}

function addPromotionInfo(itemInfoObj){
    var promotionInfo = Promotion.all()[0].barcodes;
    console.log(promotionInfo);
    for (var item of promotionInfo){
        itemInfoObj.itemsInfo[item].promotion = 1;
    }
    return itemInfoObj;
}

function getNameNum(item){
    var nameNumObj = {};
    if(item.indexOf('-') == -1){
        nameNumObj.name = item;
        nameNumObj.num = 1;
    }else{
        nameNumObj.name = item.substring(0 , item.indexOf('-'));
        nameNumObj.num = Number(item.substring(item.indexOf('-')+1));
    }
    return nameNumObj;
}

function createBuyList(itemInfoObj,tags){
    var itemBuyObj = new itemBuyContainer();
    
    tags.forEach(function(item){
        itemBuyObj.buyListPush(itemInfoObj.itemsInfo[getNameNum(item).name],getNameNum(item).num); 
    });
    console.log(itemBuyObj);
    return itemBuyObj;
}

function  calculateMoney(itemBuyObj){
    itemBuyObj.buyList.forEach(function(item){
        item.pay = (item.buyNum - Math.floor(item.buyNum/3) * item.promotion) * item.price;
        itemBuyObj.paySum += item.pay;
        itemBuyObj.saveMoney += Math.floor(item.buyNum/3) * item.promotion * item.price;
    });
    return itemBuyObj;
}

function createString(itemBuyObj){
    const dateDigitToString = num => (num < 10 ? `0${num}` : num);
    const currentDate = new Date(),
      year = dateDigitToString(currentDate.getFullYear()),
      month = dateDigitToString(currentDate.getMonth() + 1),
      date = dateDigitToString(currentDate.getDate()),
      hour = dateDigitToString(currentDate.getHours()),
      minute = dateDigitToString(currentDate.getMinutes()),
      second = dateDigitToString(currentDate.getSeconds()),
      formattedDateString = `${year}年${month}月${date}日 ${hour}:${minute}:${second}`;

    var string = '***<没钱赚商店>收据***\n打印时间：' + formattedDateString + '\n----------------------';
    itemBuyObj.buyList.forEach(function(item){
        string += '\n名称：' + item.name +
                    '，数量：' + item.buyNum + item.unit +
                    '，单价：' + item.price.toFixed(2) +  
                    '(元)，小计：' + item.pay.toFixed(2) + '(元)';
    });
    string += '\n----------------------\n总计：' + itemBuyObj.paySum.toFixed(2) +
                '(元)\n节省：' + itemBuyObj.saveMoney.toFixed(2) + '(元)\n**********************';
    return string;
}

function printReceipt(tags){
    var itemInfoObj = createItemInfo();
    itemInfoObj = addPromotionInfo(itemInfoObj);
    var itemBuyObj = createBuyList(itemInfoObj,tags);
    itemBuyObj = calculateMoney(itemBuyObj);
    var string = createString(itemBuyObj);

    console.log(string);
}


//TODO: 请在该文件中实现练习要求并删除此注释

// function createItemInfo(){
//     var array = Item.all();
//     var itemInfo = {};
//     var itemIndex = {};
    
//     for (var i = 0; i < array.length; i++){
//         var obj = {};
//         obj.name = array[i].name;
//         obj.unit = array[i].unit;
//         obj.price = array[i].price;
//         obj.promotion = 0;
//         obj.buyNum = 0;
//         obj.pay = 0;
//         itemIndex[array[i].barcode] = obj;
//     }
//     itemInfo.itemIndex = itemIndex;
//     itemInfo.buyWhat = [];
//     itemInfo.paySum = 0;
//     itemInfo.saveMoney = 0;

//     return itemInfo;
// }

// function readTagsInfo(itemInfo,tags){
//     for (var i = 0; i < tags.length; i++){
//         if(tags[i].indexOf('-') == -1){
//             if(itemInfo.itemIndex[tags[i]].buyNum == 0){
//                 itemInfo.buyWhat.push(tags[i]);
//             }
//             itemInfo.itemIndex[tags[i]].buyNum += 1;
//         }else{
//             var nameStr = tags[i].substring(0 , tags[i].indexOf('-'));
//             var numStr = tags[i].substring(tags[i].indexOf('-')+1);
//             if(itemInfo.itemIndex[nameStr].buyNum == 0){
//                 itemInfo.buyWhat.push(nameStr);
//             }
//             itemInfo.itemIndex[nameStr].buyNum += Number(numStr);
//         }
//     }
//     return itemInfo;
// }

// function revisePromotionValue(itemInfo){
//     var promot = Promotion.all()[0].barcodes;
//     console.log(promot);
//     for (var i = 0; i < promot.length; i++){
//         itemInfo.itemIndex[promot[i]].promotion = 1;
//     }
//     return itemInfo;
// }

// function calculateMoney(itemInfo){
//     for (var i = 0; i < itemInfo.buyWhat.length; i++){
//         if(itemInfo.itemIndex[itemInfo.buyWhat[i]].promotion == 1 && itemInfo.itemIndex[itemInfo.buyWhat[i]].buyNum >= 3){
//             itemInfo.itemIndex[itemInfo.buyWhat[i]].pay = (itemInfo.itemIndex[itemInfo.buyWhat[i]].buyNum - Math.floor(itemInfo.itemIndex[itemInfo.buyWhat[i]].buyNum/3)) * itemInfo.itemIndex[itemInfo.buyWhat[i]].price;
//             itemInfo.saveMoney += Math.floor(itemInfo.itemIndex[itemInfo.buyWhat[i]].buyNum/3) * itemInfo.itemIndex[itemInfo.buyWhat[i]].price;
//         }else{
//             itemInfo.itemIndex[itemInfo.buyWhat[i]].pay = itemInfo.itemIndex[itemInfo.buyWhat[i]].buyNum * itemInfo.itemIndex[itemInfo.buyWhat[i]].price;
//         }
//         itemInfo.paySum += itemInfo.itemIndex[itemInfo.buyWhat[i]].pay;
//     }
//     return itemInfo;
// }

// function createShowString(itemInfo){
//     const dateDigitToString = num => (num < 10 ? `0${num}` : num);
//     const currentDate = new Date(),
//       year = dateDigitToString(currentDate.getFullYear()),
//       month = dateDigitToString(currentDate.getMonth() + 1),
//       date = dateDigitToString(currentDate.getDate()),
//       hour = dateDigitToString(currentDate.getHours()),
//       minute = dateDigitToString(currentDate.getMinutes()),
//       second = dateDigitToString(currentDate.getSeconds()),
//       formattedDateString = `${year}年${month}月${date}日 ${hour}:${minute}:${second}`;

//     var string = '***<没钱赚商店>收据***\n打印时间：' + formattedDateString + '\n----------------------';
//     for (var i = 0; i < itemInfo.buyWhat.length; i ++){
//         string += '\n名称：' + itemInfo.itemIndex[itemInfo.buyWhat[i]].name +
//                     '，数量：' + itemInfo.itemIndex[itemInfo.buyWhat[i]].buyNum + itemInfo.itemIndex[itemInfo.buyWhat[i]].unit +
//                     '，单价：' + itemInfo.itemIndex[itemInfo.buyWhat[i]].price.toFixed(2) +  
//                     '(元)，小计：' + itemInfo.itemIndex[itemInfo.buyWhat[i]].pay.toFixed(2) + '(元)';
//     }
//     string += '\n----------------------\n总计：' + itemInfo.paySum.toFixed(2) +
//                 '(元)\n节省：' + itemInfo.saveMoney.toFixed(2) + '(元)\n**********************';
//     return string;
// }

// function printReceipt(tags){
//     var itemInfo = createItemInfo();

//     itemInfo = readTagsInfo(itemInfo,tags);
//     itemInfo = revisePromotionValue(itemInfo);
//     itemInfo = calculateMoney(itemInfo);
//     var string = createShowString(itemInfo);

//     console.log(string);
// }